<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.form.js"></script>
<script type="text/javascript">
get_template_by_shop = function(a){
	$.ajax({
		url: "<?=url('Online::EbayListing/GetTemplateByShopAjax')?>" + "&shop_id=" + a.val(),
		type: "get",
		dataType: "json",
		success: function(data){
			var _html_template = '<option value="">--请选择--</option>';
			$.each(data,function(k,v){
				_html_template += '<option value="' + v['id'] + '">' + v['name'] + '</option>';
			});
			a.parent().next().next().children().html(_html_template);
		}
	});
};
$(function(){
	$("#dialog_combine").dialog({autoOpen:false,modal:true, height: 300, width: 200});
	$(".shop_id").each(function(){
		var a = $(this);
		get_template_by_shop(a);
	});
	$(".shop_id").live("change",function(){
		var a = $(this);
		get_template_by_shop(a);
	});
	$(".delete").live("click", function(){
		$(this).parent().parent().parent().remove();
	});
	var i = "<?=isset($shop_ids) ? count($shop_ids) : 0 ?>";
	$("#add_listing").click(function(){
		var _html = "<table class='list_table'>";
		_html += "<tr><td width='5' rowspan='5'><input type='checkbox' class='is_checked' checked='checked' /></td></tr>";
		_html += "<tr><th width='10'>Folder</th><td width='30' align='left'><select name='listing[" + i + "][folder_id]' class='folder_id'><option value=''>请选择</option><?foreach ($folder as $f):?><option value='<?=$f['id']?>'><?=$f['name']?></option><?endforeach;?></select><a class='copy_to_other'>复制到其他</a></td><th width='10'>Shop</th><td width='30' align='left'><select name='listing[" + i + "][shop_id]' class='shop_id'><option value=''>请选择</option><?foreach ($shop as $s):?><option value='<?=$s['id']?>'><?=$s['name']?></option><?endforeach;?></select></td><th width='10'>Template</th><td width='30' align='left'><select name='listing[" + i + "][template_id]' class='template'></select></td><td width='20' rowspan=5><a class='delete'>删除</a></td></tr>";
		_html += "<tr><th width='10'>Title</th><td colspan='5' align='left'><input type='text' name='listing[" + i + "][title]' value='<?=htmlspecialchars($listing['title'], ENT_QUOTES)?>' class='title' size='100' /><span style='color: red;'></span></td></tr>";
		_html += "<tr><th style='vertical-align: text-top;'>Image Description<br /></th><td colspan='5' width='1000' align='left'><textarea wrap='off' name='listing[" + i + "][desc_img]' class='img_description' style='width:900px;overflow:scroll;' rows='5'></textarea><a class='copy_to_other'>复制到其他</a></td></tr>";
		_html += "</table>";
		$("#submit_form").append(_html);
		i++;
	});
	$("#verify_title_btn").click(function(){
		var icp = $("input.title"), repeat = false, length = false;
		icp.each(function(){
			if (icp.filter('[value="' + this.value + '"]').size()>1)
			{
				repeat = this.value;
				$(this).css('border','1px solid red');
			}
			if ($(this).val().length > 80)
			{
				length = true;
				$(this).next().html('Max Limit 80 chars. Now ' + $(this).val().length + ' chars');
			}
			else
			{
				$(this).next().html('');
			}
		});
		if (repeat)
		{
			$("#verify_title_btn").next().html('Title Repeat!');
		}
		else if (length)
		{
			$("#verify_title_btn").next().html('Title Length >80!');
		}
		else
		{
			$("#verify_title_btn").next().html('检测通过');
		}
	});
	$(".copy_to_other").live("click", function(){
		var copy_content = $(this).prev().val();
		$("."+$(this).prev().attr("class")).each(function(){
			$(this).val(copy_content);
		});
	});
	$("#add_save").click(function(){
		$(".list_table").find("input.is_checked").each(function(){
			if (!$(this).is(":checked"))
			{
				$(this).parent().parent().parent().parent().remove();
			}
		});
		var can_save = 1;
		$(".shop_id").each(function(){
			if (!$(this).val())
			{
				can_save = 0;
				alert("please check every shop select");
				return false;
			}
		});
		if(can_save)
		{
			$("#submit_form").submit();
		}
	});
});
</script>
<form action="<?=url('Online::EbayListing/MultiCopySave')?>" method="post" id="submit_form">
<input type='hidden' name="id" value="<?=$listing['id']?>" / >
<table class="form_table">
	<tr>
		<th>Text Description</th>
		<td colspan="5"><textarea rows="5" cols="60" name="desc_text" class="text_description" style="width: 1000px;"></textarea></td>
	</tr>
</table>
<?if (isset($shop_ids) && $shop_ids):?>
<?foreach ($shop_ids as $k => $shop_id):?>
<table class="list_table">
	<tr>
		<td width="5" rowspan=5>
			<input type='checkbox' class='is_checked' checked='checked' />
		</td>
	</tr>
	<tr>
		<th width="10">Folder</th>
		<td width="30" align="left">
			<select name="listing[<?=$k?>][folder_id]" class="folder_id">		
				<option value="">请选择</option>
				<?foreach ($folder as $f):?>
				<option value="<?=$f['id']?>" <?if (isset($folder_id) && $folder_id && $f['id'] == $folder_id):?>selected="selected"<?endif;?>><?=$f['name']?></option>
				<?endforeach;?>
			</select>
			<a class="copy_to_other">复制到其他</a>
		</td>
		<th width="10">Shop</th>
		<td width="30" align="left">
			<select name="listing[<?=$k?>][shop_id]" class="shop_id">
				<option value="">请选择</option>
				<?foreach ($shop as $s):?>
				<option value="<?=$s['id']?>"  <?if (isset($shop_id) && $s['id'] == $shop_id):?>selected="selected"<?endif;?>><?=$s['name']?></option>
				<?endforeach;?>
			</select>
		</td>
		<th width="10">Template</th>
		<td width="30" align="left">
			<select name="listing[<?=$k?>][template_id]" class="template">
			</select>
		</td>
		<td width="20" rowspan=5><a class='delete'>删除</a></td>
	</tr>
	<tr>
		<th width="10">Title</th>
		<td colspan="5" align="left"><input type="text" name="listing[<?=$k?>][title]" value="<?=htmlspecialchars($listing['title'])?>" class="title" size="100" /><span style="color: red;"></span></td>
	</tr>
	<tr>
		<th style="vertical-align: text-top;">Image Description<br /></th>
		<td colspan="5" width="1000" align="left"><textarea wrap="off" name="listing[<?=$k?>][desc_img]" class="img_description" style="width:900px;overflow:scroll;" rows="5"></textarea><a class="copy_to_other">复制到其他</a></td>
	</tr>
</table>
<?endforeach;?>
<?endif;?>
</form>
<div align="left"><a id="add_listing">添加listing</a></div>
<div align="center">
	<input type="button" class="button" id="add_save" value="保存" /><input type="hidden" name="<?=Q::ini('unique_token_name');?>" value="<?=Helper_BSS_Normal::genToken();?>"/>
	<input type="button" value="检测Title" class="button" style="width: 100px;" id="verify_title_btn" /><span style="color: red;"></span>
</div>
<?php $this->_endblock();?>